Ext.define('console.internalEval', {
    extend: 'Ext.ux.desktop.App',

    requires: [
        'Ext.window.MessageBox',
        'Ext.ux.desktop.ShortcutModel',
        'console.Settings',
        'console.passwordWindow',
        'console.userWindow',
        'console.gradeScoreWindow',
        'console.testPaperWindow',
        'console.internalEvalMapperWindow',
        'console.questionWindow',
        'console.deptGroupWindow',
        'console.deptWindow',
        'console.evalManageWindow',
        'console.evalPageWindow',
        'console.deptbindpaper',
    ],

    init: function () {
        this.callParent();
        this.checkLogin();
    },

    getModules: function () {
        return [
            new console.passwordWindow(),
            new console.userWindow(),
            new console.gradeScoreWindow(),
            new console.testPaperWindow(),
            new console.internalEvalMapperWindow(),
            new console.deptbindpaper(),
            new console.questionWindow(),
            new console.deptGroupWindow(),
            new console.deptWindow(),
            new console.evalManageWindow(),
            new console.evalPageWindow()
        ];
    },

    getDesktopConfig: function () {
        var me = this, ret = me.callParent();
        return Ext.apply(ret, {
            //cls: 'ux-desktop-black',
            contextMenuItems: [
                {text: '设置背景', handler: me.onSettings, scope: me}
            ],
            shortcuts: Ext.create('Ext.data.Store', {
                model: 'Ext.ux.desktop.ShortcutModel',

                data: [
                    {name: '账号管理', iconCls: 'grid-shortcut', module: 'passwordWindow'},
                    {name: '用户管理', iconCls: 'grid-shortcut', module: 'userWindow'},
                    {name: '分值维护', iconCls: 'grid-shortcut-tb', module: 'gradeScoreWindow'},
                    {name: '问卷管理', iconCls: 'grid-shortcut', module: 'testPaperWindow'},
                    {name: '内部互评管理', iconCls: 'grid-shortcut', module: 'internalEvalMapperWindow'},
                    {name: '问题管理', iconCls: 'grid-shortcut', module: 'questionWindow'},
                    {name: '评价问卷管理', iconCls: 'grid-shortcut', module: 'deptbindpaper'},
                    {name: '组别管理', iconCls: 'grid-shortcut', module: 'deptGroupWindow'},
                    {name: '科室管理', iconCls: 'grid-shortcut', module: 'deptWindow'},
                    {name: '评价管理', iconCls: 'grid-shortcut', module: 'evalManageWindow'},
                    {name: '评价表', iconCls: 'grid-shortcut-pjb', module: 'evalPageWindow'}
                ]
            }),

            wallpaper: 'console/wallpapers/Blue-Sencha.jpg',
            wallpaperStretch: false
        });
    },

    // config for the start menu
    getStartConfig: function () {
        var me = this, ret = me.callParent();

        return Ext.apply(ret, {
            title: '用户',
            iconCls: 'user',
            height: 300,
            toolConfig: {
                width: 100,
                items: [
                    {
                        text: '设置',
                        iconCls: 'settings',
                        handler: me.onSettings,
                        scope: me
                    },
                    '-',
                    {
                        text: '注销',
                        iconCls: 'logout',
                        handler: me.onLogout,
                        scope: me
                    }
                ]
            }
        });
    },

    getTaskbarConfig: function () {
        var ret = this.callParent();

        return Ext.apply(ret, {
            quickStart: [
                // {name: 'Accordion Window', iconCls: 'accordion', module: 'acc-win'},
                // {name: 'Grid Window', iconCls: 'icon-grid', module: 'grid-win'}
            ],
            trayItems: [
                {xtype: 'trayclock', flex: 1}
            ]
        });
    },

    checkLogin: function () {
        Ext.Ajax.request({
            url: 'user/loadCurrUserInfo',
            success: function (response) {
                var res = Ext.decode(response.responseText);
                if (res.success) {
                } else {
                    Ext.create('Ext.window.Window', {
                        modal: true,
                        title: '请登录',
                        closable: false,
                        items: [{
                            xtype: 'form',
                            defaults: {
                                labelAlign: 'left',
                                margin: '10 10 10 10',
                                width: '90%',
                                xtype: 'textfield'
                            },
                            items: [{
                                name: 'username',
                                fieldLabel: '用户名',
                                allowBlank: false
                            }, {
                                name: 'password',
                                fieldLabel: '密码',
                                inputType: 'password',
                                allowBlank: false
                            }],
                            buttons: [{
                                text: '重置',
                                handler: function () {
                                    this.up('form').getForm().reset();
                                }
                            }, {
                                text: '提交',
                                handler: function () {
                                    var form = this.up('form').getForm();
                                    if (form.isValid()) {
                                        form.submit({
                                            url: 'user/normalLogin',
                                            success: function (form, action) {
                                                form.owner.ownerCt.close();

                                            },
                                            failure: function (form, action) {
                                                var res = Ext.decode(action.response.responseText);
                                                Ext.Msg.alert('提示', res.failure);
                                            }
                                        });
                                    }
                                }
                            }]
                        }]
                    }).show()
                }
            },
            failure: function (action) {
                Ext.Msg.alert('', '访问服务失败，请联系管理员!');
            }
        });
    },

    onLogout: function () {
        Ext.Msg.confirm('注销', '您确定注销登录吗?', function (opt) {
            if (opt == 'yes') {
                Ext.Ajax.request({
                    url: 'user/logout',
                    success: function (response) {
                        var res = Ext.decode(response.responseText);
                        if (res.success) {
                            Ext.Msg.alert('提示', '注销成功');
                            window.location.href="login.jsp";
                        } else {
                            Ext.Msg.alert('操作失败', res.failure);
                        }
                    },
                    failure: function (action) {
                        Ext.Msg.alert('', '操作信息失败');
                    }
                });
            }
        });
    },

    onSettings: function () {
        var dlg = new console.Settings({
            desktop: this.desktop
        });
        dlg.show();
    }
});